package com.cjl.jvm.gc;

/**
 * -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
 *
 * -XX:PretenureSizeThreshold=3145728 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC
 */
public class GcMaster {
    private static final int _1MB = 1024 * 1024;

    /**
     * -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
     *
     *
     *
     * Heap
     *  PSYoungGen      total 9216K, used 7143K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
     *   eden space 8192K, 87% used [0x00000000ff600000,0x00000000ffcf9fa8,0x00000000ffe00000)
     *   from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
     *   to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
     *  ParOldGen       total 10240K, used 4096K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
     *   object space 10240K, 40% used [0x00000000fec00000,0x00000000ff000010,0x00000000ff600000)
     *  Metaspace       used 2787K, capacity 4486K, committed 4864K, reserved 1056768K
     *   class space    used 292K, capacity 386K, committed 512K, reserved 1048576K
     *
     * BUILD SUCCESSFUL in 487ms
     * 3 actionable tasks: 3 executed
     * 下午9:05:53: Task execution finished 'GcMaster.main()'.
     *
     *
     *  解析:
     *      对象优先Eden分配,10MB给新生代,剩余给老年代
     *      allocation1,2,3占用Eden 6MB,  4分配时不足(可用只有8+1),触发MinorGC?
     *          已有的3个对象(6MB)无法移到Survior空间(剩余1MB),执行担保机制
     *          转移到老年代
     *
     */
    public static void testAllocation(){
        byte[] allocation1,allocation2,allocation3,allocation4;
        allocation1 = new byte[2 * _1MB];
        allocation2 = new byte[2 * _1MB];
        allocation3 = new byte[2 * _1MB];
        allocation4 = new byte[4 * _1MB];
    }

    /**
     * -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
     *  -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:PretenureSizeThreshold=3145728
     *
     *
     *  Heap
     *   par new generation   total 9216K, used 999K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
     *   eden space 8192K,  12% used [0x00000000fec00000, 0x00000000fecf9ff0, 0x00000000ff400000)
     *   from space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
     *   to   space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
     *  concurrent mark-sweep generation total 10240K, used 4096K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
     *  Metaspace       used 2788K, capacity 4486K, committed 4864K, reserved 1056768K
     *   class space    used 292K, capacity 386K, committed 512K, reserved 1048576K
     *
     *  解析:
     *      大对象直接进入老年代,vm参数阈值3MB
     */
    public static void testPretenureSizeThreshold(){
        byte[] allocation;
        allocation = new byte[4 * _1MB];
    }

    public static void main(String[] args) {
        testAllocation();
//        testPretenureSizeThreshold();
    }
}
